package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.vo.AgentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAgent;
import com.ruoyi.system.service.ISysAgentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 代理商Controller
 * 
 * @author ruoyi
 * @date 2025-10-08
 */
@RestController
@RequestMapping("/system/agent")
@Anonymous
public class SysAgentController extends BaseController
{

    @Autowired
    private ISysAgentService sysAgentService;

    /**
     * 查询代理商列表
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:list')")
    @RequestMapping("/list")
    public TableDataInfo list(SysAgent sysAgent)
    {
        startPage();
        //List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        List<AgentVo> list = sysAgentService.selectAgentList(sysAgent);
        return getDataTable(list);
    }

    @RequestMapping("/{agentId}/list")
    public TableDataInfo list(@PathVariable Long agentId)
    {
        startPage();
        List<SysAgent> list = sysAgentService.level2List(agentId);
        return getDataTable(list);
    }
    /**
     * 根据用户Id获取详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:query')")
    @GetMapping(value = "/{agentId}")
    public AjaxResult getInfo(@PathVariable Long agentId)
    {
        return success(sysAgentService.selectSysAgentById(agentId));
    }
    /**
     * 导出代理商列表
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:export')")
    @Log(title = "代理商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAgent sysAgent)
    {
        List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        ExcelUtil<SysAgent> util = new ExcelUtil<SysAgent>(SysAgent.class);
        util.exportExcel(response,list, "代理商数据");
    }

    /**
     * 新增保存代理商
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:add')")
    @Log(title = "代理商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody SysAgent sysAgent)
    {
        return toAjax(sysAgentService.insertSysAgent(sysAgent));
    }

    /**
     * 修改保存代理商
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:edit')")
    @Log(title = "代理商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody SysAgent sysAgent)
    {
        return toAjax(sysAgentService.updateSysAgent(sysAgent));
    }

    /**
     * 删除代理商
     */
    //@PreAuthorize("@ss.hasPermi('system:agent:remove')")
    @Log(title = "代理商", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAgentService.deleteSysAgentByIds(ids));
    }
}
